package com.longrise.bymodule.page.byzp;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.bymodule.R;
import com.longrise.bymodule.constant.ByModuleConstants;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;
import com.longrise.common.base.web.BaseWebClient;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.publicpage.TipsActivity;
import com.longrise.common.router.RouterPage;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;
import com.umeng.socialize.UMShareAPI;

/**
 * Created by Administrator on 2017/11/24.
 * 新框架下的保易智评
 */

@Route(path = RouterPage.BYZPActivity)
public class BYZPActivity extends BaseWebActivity {


    private WebView mWv;
    private Toolbar mTitle;
    //    private RelativeLayout mRight;
    private ImageView mRightShare;

    private boolean mFlag;

    private String mByjkUserid; //保易健康的userid
    private String mByjkCardno;
    private String mByjkMobilephone;
    private String mByjkUsername;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.by_module_activity_byzp;
    }

    @Override
    public WebView getWebView() {
        return mWv;
    }

    @Override
    public void init() {
        mWv = (WebView) findViewById(R.id.activity_xqpc_webview);
        mTitle = (Toolbar) findViewById(R.id.byjk_toolbar);
        mRightShare = (ImageView) mTitle.findViewById(R.id.iv_toolbar_share);
        setWebTitle("保易网");
        setToolbarBackIcon(R.drawable.umeng_by_ic_close_black);
        mFlag = true;
        setInterceptBackKey(false);
    }

    @Override
    public void onToolbarBackClick() {
        super.onToolbarBackClick();
        finish();
    }

    @Override
    public void onToolbarRightIvClick() {
        super.onToolbarRightIvClick();
//        PrintLog.e("BYZPActivity","1111111111111111");
        toShare();
    }

    @Override
    public void initData() {
        mByjkUserid = getIntent().getStringExtra("userid");
        mByjkCardno = getIntent().getStringExtra("cardno");
        mByjkMobilephone = getIntent().getStringExtra("mobilephone");
        mByjkUsername = getIntent().getStringExtra("username");

        request();
    }

    @Override
    public BaseWebBridge getwebBridge() {
        return new BYZPwebBridge(this, mRxmanager);
    }

    @Override
    public BaseWebClient getWebClient() {
        return new BYZPWebClient(this);
    }

    public void setImageVisible(boolean isVisiable, String title) {
        if (isVisiable) {
            setToolbarRightIvTitle(R.drawable.by_ic_share);
        } else {
            hideToolbarRightIvTitle();
        }
        showTips(title);
    }


    private void request() {
        int screenDp = 0;
        try {
            int topHeight = AppUtil.getStatusBar(this);
            int screenHeight = AppUtil.getScreenHeight();
            screenDp = AppUtil.px2dip(screenHeight - topHeight);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EntityBean entityBean = new EntityBean();
        entityBean.set("userid", mByjkUserid);
        entityBean.set("cardno", mByjkCardno);
        entityBean.set("username", mByjkUsername);
        entityBean.set("mobilephone", mByjkMobilephone);
        entityBean.set("screenheight", screenDp - 45);
        entityBean.set("page", 2);
        EntityBean reqBean = new EntityBean();
        reqBean.set("bean", entityBean);
        LoadDataManager.getInstance().callService(null, ByModuleConstants.BY_URL,
                "app_isauthorization", reqBean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            clientParse((EntityBean) result);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        mUrl = "";
                    }

                    @Override
                    public void onFinished(String key, String service) {
                    }

                });
    }

    private void clientParse(EntityBean result) {
        EntityBean bean = result.getBean("result");
        mUrl = bean.getString("href");
        if (isEmptyUrl()) {
            loadUrl(mUrl);
        }
    }

    private boolean isEmptyUrl() {
        if (TextUtils.isEmpty(mUrl)) {
            mUrl = "";
            return false;
        } else {
            return true;
        }
    }

    /**
     * 新手提示
     */
    public void showTips(String title) {
//        暂时不需要判断,每次进来都需要提示.
//        boolean b = Control.isbyShare();
//        if(!b){
//          return ;
//        }
        if (!mFlag) {
            return;
        }
        boolean e = "保易网需求分析测试".equals(title);
        PrintLog.e("BYZPActivity", "e=:" + e);
        if (e) {
            int[] loc1 = new int[2];
            int width = AppUtil.dip2px(15);
            int height = AppUtil.dip2px(37);
            PrintLog.e("BYZPActivity", "width=:" + width);
            PrintLog.e("BYZPActivity", "height=:" + height);
            int h1 = AppUtil.dip2px(20);
            int com = AppUtil.dip2px(8);
//            int[] loc = {loc1[0] - com, loc1[1] - h1 - com, loc1[0] + width + com, loc1[1] + height - h1 + com};
            int[] loc = {AppUtil.getScreenWidth() - AppUtil.dip2px(15) - width, AppUtil.dip2px(12), loc1[0] + width + com, loc1[1] + height - h1 + com};
            PrintLog.e("BYZPActivity", "loc=:" + loc[0]);
            Intent intent = new Intent(this, TipsActivity.class);
            intent.putExtra("type", TipsActivity.BYSHARE_TIPS);
            intent.putExtra("location", loc);
            startActivity(intent);
            mFlag = false;
            PrintLog.e("BYZPActivity", "3333333333333333");
        }
    }

    public void linkTo(String url) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri linkUrl = Uri.parse(url);
        intent.setData(linkUrl);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}
